/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mirac
 * Date: 2025-09-17
 * Time: 18:02
 */
public class Solution {
    static class ListNode {
        int val;
        ListNode next = null;

        ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode FindKthToTail(ListNode head,int k) {
        if (head == null || k <= 0) {
            return null;
        }
        ListNode fast = head;  //快指针
        ListNode slow = head;  //慢指针

        //快指针先走k步
        while (k != 0) {
            if (fast == null) {
                return null;
            }else {
                fast = fast.next;
            }
            k--;
        }

        //快慢指针一起走，当快指针停下时，慢指针所指向节点就是倒数k个结点
        while (fast != null) {
            slow = slow.next;
            fast = fast.next;
        }

        return slow;
    }
}
